package org.example.tree;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.example.bean.GameMap;

import java.awt.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName QuadtreeDemo
 * @Description TODO
 * @Author hongjun
 * @Date 2024/5/17 下午10:42
 * @Version 1.0
 */
public class QuadtreeDemo {

    private static final String CONFIG_DIR = "src/main/resources/MapTilesData.json";

    public static void main(String[] args) {

        Rectangle mapBounds = new Rectangle(0, 0, 121, 121); // 地图边界
        Quadtree quadtree = new Quadtree(mapBounds, 0, 4); // 创建四叉树，每个节点最多存储4个阻挡物
        initMap(quadtree);
        // 添加阻挡物到四叉树中
        Rectangle rectBounds = new Rectangle(1000, 1000, 500, 500);
        Block rectBlock = new Block(Block.Shape.RECTANGLE, rectBounds, null);
        quadtree.addBlock(rectBlock);

        int centerX = 1500, centerY = 1500, radius = 250;
        Circle circle = new Circle(centerX, centerY, radius);
        Rectangle circleBounds = new Rectangle(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
        Block circleBlock = new Block(Block.Shape.CIRCLE, circleBounds, circle);
        quadtree.addBlock(circleBlock);

        // 显示QuadtreeDrawer在一个JFrame中
        QuadtreeDrawer.showInFrame(quadtree);
        // 查询指定区域内的阻挡物
        Rectangle queryRange = new Rectangle(60, 60, 60, 60);
        List<Block> blocksInRange = quadtree.queryRange(queryRange);
        System.out.println("查询范围内的阻挡物数量: " + blocksInRange.size());

        // ... 可以继续添加其他操作，如移动、移除阻挡物等 ...
        List<AStarPathfinding.Node> path = AStarPathfinding.findPath(new AStarPathfinding.Node(1, 1), new AStarPathfinding.Node(111, 111), quadtree);
        System.out.println(path);
    }

    /**
     * 生成地图数据
     *
     * @param quadtree
     */
    private static void initMap(Quadtree quadtree) {
        String mapJson = getConfigAsString();
        JSONObject jsonObject = JSONObject.parseObject(mapJson);
        Object tilesDatas = jsonObject.get("tilesDatas");
        int count =0;
        if (tilesDatas instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) tilesDatas;
            for (Object o : jsonArray) {
                JSONObject tile = (JSONObject) o;
                int value = Integer.parseInt((String) tile.get("_func"));
                if (value != 0) {
                    continue;
                }
                String pos = (String) tile.get("_key");
                List<Integer> integers = convertIdsToIntList(pos, ",");
                Integer x = integers.get(0);
                Integer y = integers.get(1);
                Rectangle rectBounds = new Rectangle(x, y, 1, 1);
                Block rectBlock = new Block(Block.Shape.RECTANGLE, rectBounds, null);
                quadtree.addBlock(rectBlock);
                count ++;
            }
        }
        System.out.println(count);
    }

    /**
     * 读取地图配置
     *
     * @return
     */
    public static String getConfigAsString() {
        String configPath = CONFIG_DIR;
        try {
            byte[] configBytes = Files.readAllBytes(Paths.get(configPath));
            return new String(configBytes);
        } catch (IOException e) {
            e.printStackTrace();
            return "读取异常";
        }
    }

    /**
     * 分割
     *
     * @param ids
     * @param key
     * @return
     */
    public static List<Integer> convertIdsToIntList(String ids, String key) {
        List<Integer> list = new ArrayList<>();
        if (ids == null || ids.length() == 0) {
            return list;
        }
        String[] ps = ids.split(key);
        for (int i = 0; i < ps.length; i++) {
            int id = Integer.parseInt(ps[i]);
            if (id == -1) {
                continue;
            }
            list.add(id);
        }
        return list;
    }
}
